const TARGET_PATHS = ['/workflow/view/', '/workflow/process/detail/', '/workflow/process/detailTask/', '/workflow/process/start/', '/workflow/process/detailFubaoTempSave/']
const BUSINESS_PAGE_SEARCH_PARAMS_KEY = 'BUSINESS_PAGE_SEARCH_PARAMS'

export default {
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (TARGET_PATHS.some(p => { return from.path && from.path.startsWith(p) })) {
        const cachedSearchParams = sessionStorage.getItem(BUSINESS_PAGE_SEARCH_PARAMS_KEY);
        if (cachedSearchParams) {
          const queryParams = JSON.parse(cachedSearchParams);
          if (to.path === queryParams.fromPath) {
            console.log('beforeRouteEnter BUSINESS_PAGE_SEARCH_PARAMS cache cachedSearchParams: ', queryParams)
            vm.queryParams = queryParams;
          } else {
            sessionStorage.removeItem(BUSINESS_PAGE_SEARCH_PARAMS_KEY)
          }
        }
      }
    })
  },
  beforeRouteLeave(to, from, next) {
    if (TARGET_PATHS.some(p => { return to.path && to.path.startsWith(p) })) {
      this.queryParams.fromPath = from.path
      console.log('beforeRouteLeave BUSINESS_PAGE_SEARCH_PARAMS cache queryParams: ', this.queryParams)
      sessionStorage.setItem(BUSINESS_PAGE_SEARCH_PARAMS_KEY, JSON.stringify(this.queryParams))
    } else {
      sessionStorage.removeItem(BUSINESS_PAGE_SEARCH_PARAMS_KEY)
    }
    next()
  },
}